package q337

import (
	"golearn/algorithm"
)

/**
* Definition for a binary tree node.
* type TreeNode struct {
*     Val int
*     Left *TreeNode
*     Right *TreeNode
* }
 */

func rob(root *algorithm.TreeNode) int {
	if root == nil {
		return 0
	}

	//情况1：节点值计算
	n1 := root.Val + robNoVal(root.Left) + robNoVal(root.Right)
	n2 := rob(root.Left) + rob(root.Right)

	if n1 >= n2 {
		return n1
	}

	return n2
}

func robNoVal(root *algorithm.TreeNode) int {
	if root == nil {
		return 0
	}

	return rob(root.Left) + rob(root.Right)
}
